package com.example.wechat.activity;

import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager2.widget.ViewPager2;
import com.example.wechat.R;
import com.example.wechat.adapter.ImageViewerAdapter;
import java.util.ArrayList;
import java.util.List;

public class ImageViewerActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private ImageViewerAdapter adapter;
    private List<String> images;
    private int initialPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_viewer);

        // 获取传递的数据
        images = getIntent().getStringArrayListExtra("images");
        initialPosition = getIntent().getIntExtra("position", 0);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle((initialPosition + 1) + "/" + images.size());

        // 初始化ViewPager2
        viewPager = findViewById(R.id.view_pager);
        adapter = new ImageViewerAdapter(images);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(initialPosition);

        // 监听页面切换
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                getSupportActionBar().setTitle((position + 1) + "/" + images.size());
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 